package com.yjt.fresh.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyMyCard;
import com.yjt.fresh.api.ApiGetMyRechargeCardList;
import com.yjt.fresh.api.ApiGetRechargeCardList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanMyCard;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AtyMyCard extends BaseSimpleActivity {
    private static final int IS_USER = 1;
    private static final int NO_USER = 0;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvAppAtyMyCardWait)
    TextView mTvWait;
    @BindView(R.id.vAppAtyMyCardWait)
    View mVWait;
    @BindView(R.id.rlAppAtyMyCardWait)
    RelativeLayout mRlWait;
    @BindView(R.id.tvAppAtyMyCardAlready)
    TextView mTvAlready;
    @BindView(R.id.vAppAtyMyCardAlready)
    View mVAlready;
    @BindView(R.id.rlAppAtyMyCardAlready)
    RelativeLayout mRlAlready;
    @BindView(R.id.rvAppAtyMyCard)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyMyCard)
    SwipeRefreshLayout mSrl;
    private AdpRvAtyMyCard mAdpMyCard;
    private int isUse;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_my_card;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        getData();
    }

    private void getData() {
        ApiGetMyRechargeCardList apiGetRechargeCardList = new ApiGetMyRechargeCardList(new HttpOnNextListener<List<BeanMyCard>>() {

            @Override
            public void onNext(List<BeanMyCard> beanMyCards) {
                mAdpMyCard.setNewData(beanMyCards);
            }

            @Override
            public void onReLogin() {
                toLogin();
            }
        },mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("ISUSED",isUse);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetRechargeCardList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetRechargeCardList);

    }

    private void initClick() {
        mAdpMyCard.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppRecycleItemMyCardCopy){
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    // 将文本内容放到系统剪贴板里。
                    cm.setText(mAdpMyCard.getItem(position).getPASSWORD());
                    Toast.makeText(mContext, "复制成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mRlAlready.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvAlready.setTextColor(getResources().getColor(R.color.color_43b624));
                mVAlready.setVisibility(View.VISIBLE);

                mTvWait.setTextColor(getResources().getColor(R.color.color_333333));
                mVWait.setVisibility(View.GONE);
                isUse = IS_USER;
                getData();
                mAdpMyCard.setUse(true);
            }
        });

        mRlWait.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvWait.setTextColor(getResources().getColor(R.color.color_43b624));
                mVWait.setVisibility(View.VISIBLE);

                mTvAlready.setTextColor(getResources().getColor(R.color.color_333333));
                mVAlready.setVisibility(View.GONE);
                isUse = NO_USER;
                getData();
                mAdpMyCard.setUse(false);
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initRv() {
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        List<BeanMyCard> list = new ArrayList<>();
        mAdpMyCard = new AdpRvAtyMyCard(R.layout.app_recycle_item_my_card,list);
        mRv.setAdapter(mAdpMyCard);
        mAdpMyCard.bindToRecyclerView(mRv);
        mAdpMyCard.setEmptyView(R.layout.view_empty);
    }
}
